package com.wzy.alllearning.config;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

@Configuration
@EnableAsync
public class ExecutorConfig
{
	/**
	 * 使用方法 ， 在方法上使用 @Async("exportServiceExecutor(线程池名字)")
	 *
	 * @return
	 */
	@Bean("exportServiceExecutor")
	public Executor exportServiceExecutor()
	{
		final ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
		int cpuCount = Runtime.getRuntime().availableProcessors();
		threadPoolTaskExecutor.setCorePoolSize(cpuCount);
		threadPoolTaskExecutor.setMaxPoolSize(2 * cpuCount);
		threadPoolTaskExecutor.setQueueCapacity(Integer.MAX_VALUE);
		threadPoolTaskExecutor.setThreadNamePrefix("export-");
		threadPoolTaskExecutor.setRejectedExecutionHandler(new AbortPolicy());
		threadPoolTaskExecutor.initialize();
		return threadPoolTaskExecutor;
	}

}
